package com.syslec.tools;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.NoSuchPaddingException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import utils.SimpleDESUtil;

@SuppressWarnings({"serial","deprecation"})
public class EncrypDecryTool extends JFrame{
	private SimpleDESUtil simpleDesUtil;
	EncrypDecryTool() throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException{  
		simpleDesUtil = new SimpleDESUtil();
        JPanel panel = new JPanel();
        /*
		 * 调用用户定义的方法并添加组件到面板
		 */
		placeComponents(panel);
		
		this.setTitle("加密解密小工具");
		this.setSize(350, 200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().add(panel);
		/*设置界面可见*/
		this.setVisible(true);
		
		/*定义工具包*/
		Toolkit kit = Toolkit.getDefaultToolkit(); 
		/*获取屏幕的尺寸*/
		Dimension screenSize = kit.getScreenSize();
		/*获取屏幕的宽*/
		int screenWidth = screenSize.width/2;
		/*获取屏幕的高*/
		int screenHeight = screenSize.height/2;
		int height = this.getHeight();
		int width = this.getWidth();
		setLocation(screenWidth-width/2, screenHeight-height/2);
    }  
	
	private void placeComponents(final JPanel panel) {
		/*
		 * 布局部分我们这边不多做介绍 这边设置布局为 null
		 */
		panel.setLayout(null);
		// 输入密码的文本域
		JLabel passwordLabel = new JLabel("Password:");
		passwordLabel.setBounds(10, 50, 80, 25);
		panel.add(passwordLabel);

		/*
		 * 这个类似用于输入的文本域 但是输入的信息会以点号代替，用于包含密码的安全性
		 */
		final JPasswordField passwordText = new JPasswordField(20);
		passwordText.setBounds(100, 50, 165, 25);
		panel.add(passwordText);

		JButton encrypt_button = new JButton("加密");
		encrypt_button.setBounds(10, 110, 80, 25);
		
		JButton decrypt_button = new JButton("解密");
		decrypt_button.setBounds(100, 110, 80, 25);
		
		JButton close = new JButton("关闭");
		close.setBounds(190, 110, 80, 25);
		
		panel.add(encrypt_button);
		panel.add(decrypt_button);
		panel.add(close);
		encrypt_button.addActionListener(new ActionListener(){  
			public void actionPerformed(ActionEvent e){  
            	try {
            		/**创建文本框，以便弹出框中可以复制内容*/
            		JTextField jf = new JTextField();
            		jf.setText(simpleDesUtil.encryptBase64(passwordText.getText()));
            		jf.setEditable(false);
					JOptionPane.showMessageDialog(panel, jf, "加密结果",JOptionPane.WARNING_MESSAGE);
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(panel, e1.getMessage(), "错误",JOptionPane.WARNING_MESSAGE);
				}
            }  
        });
		decrypt_button.addActionListener( new ActionListener(){  
            public void actionPerformed(ActionEvent e) {  
            	try {
					JOptionPane.showMessageDialog(panel, simpleDesUtil.decryptBase64(passwordText.getText()), "解密结果",JOptionPane.WARNING_MESSAGE);
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(panel, e1.getMessage(), "错误",JOptionPane.WARNING_MESSAGE);
				}  
            }  
        }); 
		close.addActionListener( new ActionListener(){  
            public void actionPerformed(ActionEvent e) {  
            	System.exit(0);  
            }  
        }); 
	}
      
    public static void main(String args[]) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException{  
        new EncrypDecryTool();  
    }  
}
